<?php
$host = '127.0.0.1';
$port = 3128;
$password = 'password';
$password = 'admin_password';
$userid = 'rendonguid';
$username = 'rendongname';

$touserid = 'userid_5';

ini_set("memory_limit", "1024M");



$fp = fsockopen($host, $port, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
	$message = s(100) . str($userid) . str($username) . str($password);
	$out = i(strlen($message)) . $message;

    fwrite($fp, $out);

	$len = current(unpack("i", fread($fp, 4)));
	echo 'len:' . $len;
	$data = unpack("stype/istatus", fread($fp, $len));
	echo "return type:{$data['type']}, status:{$data['status']}\n";


	// 加入频道
	$message = s(102) . str("testchannel");
	$out = i(strlen($message)) . $message;
	fwrite($fp, $out);
	$len = current(unpack("i", fread($fp, 4)));
	$data = unpack("stype/istr_len", fread($fp, 6));
	$str_channel = current(unpack("a" . $data['str_len'], fread($fp, $data['str_len'])));

	echo "channel :{$data['type']}, name:{$str_channel}\n";

	// 查询好友状态
	$friend_count = 6;
	$message = s(101) . i($friend_count);
	for($i = 0; $i<$friend_count; $i++)
	{
		$message .= str("userid_" . $i);
	}

	$out = i(strlen($message)) . $message;
	fwrite($fp, $out);
	$len = current(unpack("i", fread($fp, 4)));
	$data = unpack("stype/ifriend_len", fread($fp, 6));
	echo "friend_online: len:{$data['friend_len']}";
	for($i=0; $i < $data['friend_len']; $i++)
	{
		$len_fid = current(unpack("i", fread($fp, 4)));
		$str_fid = current(unpack("a" . $len_fid, fread($fp, $len_fid)));
		
		echo $str_fid . " ";
	}
	echo "\n";
	// 发送聊天内容
	$channel = 'world';
	$content = "hello ch";
	$message = s(103) . str($channel) . str($content); // 频道聊天

	$out = i(strlen($message)) . $message;
	fwrite($fp, $out);

	// 发送聊天内容
	$content = "ni hao dd";
	$message = s(104) . str($touserid) . str($content); // 私聊

	$out = i(strlen($message)) . $message;
	fwrite($fp, $out);

	// 发送管理命令
	$content = "admin message";
	$type = 'type';
	$message = s(105) . str($type) . str($content);

	$out = i(strlen($message)) . $message;
	fwrite($fp, $out);	

exit;
	while(true){
	$len = current(unpack("i", fread($fp, 4)));// %d%hd %d%s%d%s%d%s
	$type = current(unpack("s", fread($fp, 2)));
//// len+type+srcUserID+srcUserName+name+message
	$len_uid = current(unpack("i", fread($fp, 4)));
	$str_uid = current(unpack("a" . $len_uid, fread($fp, $len_uid)));
	
	$len_username = current(unpack("i", fread($fp, 4)));
	$str_username = current(unpack("a" . $len_username, fread($fp, $len_username)));
	
	$len_name = current(unpack("i", fread($fp, 4)));
	$str_name = current(unpack("a" . $len_name, fread($fp, $len_name)));

	$len_msg = current(unpack("i", fread($fp, 4)));
	$str_msg = current(unpack("a" . $len_msg, fread($fp, $len_msg)));
	
	echo "message,type:{$type} uid:{$str_uid} username:{$str_username} name:{$str_name} msg:{$str_msg}\n";
	}

    fclose($fp);
}

// 封装字符串
function str($str)
{
	return pack("i",strlen($str)) .   $str;
}

// 封装int
function i($i)
{
	return pack("i", $i);
}

// 封装short
function s($s)
{
	return pack("s", $s);
}
?>
